package edu.pku.sei.transformation.qvt.adapter.standardqvt;

import java.util.ArrayList;
import java.util.List;

import edu.pku.sei.modeler.model.core.MetaAttribute;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaReference;
import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.Key;
import edu.pku.sei.transformation.editor.model.ModelType;
import edu.pku.sei.transformation.editor.model.TypedModel;
import edu.pku.sei.transformation.qvt.rule.IKey;

public class KeyAdapter implements IKey {
	public Key element;

	public KeyAdapter(Key key) {
		this.element = key;
	}

	@Override
	public List<String> getAttributes() {
		ArrayList<String> attributes = new ArrayList<String>();
		for (Object obj : element.getKeyAttributes()) {
			if (obj instanceof MetaReference) {
				attributes.add(((MetaReference) obj).getName());
			} else if (obj instanceof MetaAttribute) {
				attributes.add(((MetaAttribute) obj).getName());
			} else {
				attributes.add(obj.toString());
			}
		}
		return attributes;
	}

	@Override
	public String getClassName() {
		return element.getKeyClass().getName();
	}

	@Override
	public String getMetaModelName() {
		MetaModel keyMetaModel = element.getKeyClass().getModel();
		AtomicTransformation trans = (AtomicTransformation) element
				.getContainer();
		for (Object obj : trans.getTypedModel()) {
			TypedModel typedModel = (TypedModel) obj;
			ModelType type = (ModelType) typedModel.getType();
			MetaModel model = (MetaModel) type.getType();
			if (model == keyMetaModel)
				return type.getName();
		}
		return "";
	}

}
